package com.hig.questions.controller;

import java.util.*;
import java.util.stream.Collectors;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.enums.BusinessType;
import com.hig.questions.domain.ExamQuestionsList;
import com.hig.questions.domain.ExamQuestionsProperty;
import com.hig.questions.service.IExamQuestionsListService;
import com.hig.questions.service.IExamQuestionsPropertyService;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题目属性Controller
 * 
 * @author qnsdt
 * @date 2022-12-28
 */
@RestController
@RequestMapping("/questions/questionslist")
public class ExamQuestionsListController extends BaseController
{
    @Autowired
    private IExamQuestionsListService examQuestionsListService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询题目属性列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ExamQuestionsList examQuestionsList)
    {
        startPage();
        Map<String, Object> params = new HashMap<>();
        params.put("bankCode", examQuestionsList.getBankCodes()); // 传入的 bankCode 数组
        params.put("tfCount", examQuestionsList.getTfCount());
        params.put("singleCount", examQuestionsList.getSingleCount());
        params.put("multiCount", examQuestionsList.getMultiCount());
        Integer forceDone = examQuestionsList.getForceDone();
        List<ExamQuestionsList> list=new ArrayList<>();
        if (!ObjectUtils.isEmpty(forceDone)&&forceDone==1){
            List<String> selectedQuestions = examQuestionsList.getSelectedQuestions();
            if (CollectionUtils.isEmpty(selectedQuestions)){
                throw new RuntimeException("您选择的题库中无可用题目可分配，请重新选择");
            }
//            list = examQuestionsListService.selectExamQuestionsListLuanxu(Arrays.asList(examQuestionsList.getBankCodes()),examQuestionsList.getTfCount(),examQuestionsList.getSingleCount(),examQuestionsList.getMultiCount());
            list = examQuestionsListService.selectExamQuestionsListForce(selectedQuestions.toArray(new String[0]));
        }else {
            list = examQuestionsListService.selectExamQuestionsList(examQuestionsList.getBankCodes());
        }
//        if (!ObjectUtils.isEmpty(forceDone)&&forceDone==1){
//            List<ExamQuestionsList> temp=new ArrayList<>();
//            //随机出试题
//            Integer singleCount = examQuestionsList.getSingleCount();//2
//            Integer multiCount = examQuestionsList.getMultiCount();//3
//            Integer tfCount = examQuestionsList.getTfCount();  //1
//
//
//            // 过滤出 type == 1 的数据
//            List<ExamQuestionsList> tfList = list.stream()
//                    .filter(q -> q.getQuestionsType() == 1)
//                    .collect(Collectors.toList());
//
//            // 随机打乱顺序
//            Collections.shuffle(tfList);
//
//            // 取前 5 条
//            List<ExamQuestionsList> randomlistTf = tfList.stream()
//                    .limit(tfCount)
//                    .collect(Collectors.toList());
//            temp.addAll(randomlistTf);
//
//
//
//            // 过滤出 type == 1 的数据
//            List<ExamQuestionsList> singleList = list.stream()
//                    .filter(q -> q.getQuestionsType() == 2)
//                    .collect(Collectors.toList());
//
//            // 随机打乱顺序
//            Collections.shuffle(tfList);
//
//            // 取前 5 条
//            List<ExamQuestionsList> randomlistSingle = singleList.stream()
//                    .limit(singleCount)
//                    .collect(Collectors.toList());
//            temp.addAll(randomlistSingle);
//
//
//
//
//            // 过滤出 type == 1 的数据
//            List<ExamQuestionsList> multiList = list.stream()
//                    .filter(q -> q.getQuestionsType() == 3)
//                    .collect(Collectors.toList());
//
//            // 随机打乱顺序
//            Collections.shuffle(tfList);
//
//            // 取前 5 条
//            List<ExamQuestionsList> randomlistMulti = multiList.stream()
//                    .limit(multiCount)
//                    .collect(Collectors.toList());
//            temp.addAll(randomlistMulti);
//list=temp;

//        }
        return getDataTable(list);
    }
    @GetMapping("/listNoPage")
    public TableDataInfo listNoPage(ExamQuestionsList examQuestionsList)
    {
        List<ExamQuestionsList>   list = examQuestionsListService.selectExamQuestionsList(examQuestionsList.getBankCodes());
        return getDataTable(list);
    }
    @GetMapping("/selectlist/{examCode}")
    public AjaxResult selectList(@PathVariable("examCode") String examCode)
    {
        List<ExamQuestionsProperty> list = examQuestionsListService.selectChoiceExamQuestionsList(examCode);
        return AjaxResult.success(list);
    }


}
